package com.common.transformer;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.commons.lang.StringUtils;

import com.applicationLayer.formbean.PathForm;
import com.common.entity.Path;

public class PathTransformer {

	
	/*******************************
	 * Transformer
	 *******************************/
	public static Path PathForm_TO_Path(PathForm pathForm){
		Path path=new Path();
		path.setId_path(pathForm.getId_path());
		path.setPrice(Float.parseFloat(pathForm.getPrice()));
		int estimateHours = 0;
		int estimateMinutes = 0;
		if(!StringUtils.isBlank(pathForm.getEstimateHours())){
			estimateHours = Integer.parseInt(pathForm.getEstimateHours());
		}
		if(!StringUtils.isBlank(pathForm.getEstimateMinutes())){
			estimateMinutes = Integer.parseInt(pathForm.getEstimateMinutes());
		}
		path.setTime(60*estimateHours+estimateMinutes);
		if(StringUtils.isBlank(pathForm.getKm())){
			path.setKm(0);
		}else{
			path.setKm(Integer.parseInt(pathForm.getKm()));
		}
		path.setNb_places(Integer.parseInt(pathForm.getNb_places()));
		//TODO : adding hour
		//Date
		GregorianCalendar calendar = new java.util.GregorianCalendar();
		calendar.setTime(pathForm.getDate());
		//hours
		GregorianCalendar calendar_hour = new java.util.GregorianCalendar();
		if(pathForm.getHour()!=null)
		{
			calendar_hour.setTime(pathForm.getHour());
			calendar.set(Calendar.HOUR_OF_DAY, calendar_hour.get(Calendar.HOUR_OF_DAY));
			calendar.set(Calendar.MINUTE, calendar_hour.get(Calendar.MINUTE));
		}
		Date departDate = calendar.getTime();
		path.setDate(departDate);
		path.setDepart_town(pathForm.getDepart_town());
		path.setArrival_town(pathForm.getArrival_town());
		path.setDriver(pathForm.getDriver());
		return path;
	}

	public static PathForm Path_TO_PathForm(Path p) {
		PathForm pathForm = new PathForm();
		pathForm.setArrival_town(p.getArrival_town());
		pathForm.setDate(p.getDate());
		pathForm.setDepart_town(p.getDepart_town());
		pathForm.setDriver(p.getDriver());
		pathForm.setId_path(p.getId_path());
		pathForm.setKm(Integer.toString(p.getKm()));
		pathForm.setNb_places(Integer.toString(p.getNb_places()));
		pathForm.setPrice(Float.toString(p.getPrice()));
		pathForm.setEstimateHours(Integer.toString((int)Math.floor(p.getTime()/60)));
		pathForm.setEstimateMinutes(Integer.toString(p.getTime()%60));
		pathForm.setHour(p.getDate());
		return pathForm;
	}

	public static PathForm Path_TO_PathForm(Path p, String departure, String arrival,String driver) {
		PathForm pathForm = new PathForm();
		pathForm.setArrival_town(p.getArrival_town());
		pathForm.setDate(p.getDate());
		pathForm.setDepart_town(p.getDepart_town());
		pathForm.setDriver(p.getDriver());
		pathForm.setId_path(p.getId_path());
		pathForm.setKm(Integer.toString(p.getKm()));
		pathForm.setNb_places(Integer.toString(p.getNb_places()));
		pathForm.setPrice(Float.toString(p.getPrice()));
		pathForm.setEstimateHours(Integer.toString((int)Math.floor(p.getTime()/60)));
		pathForm.setEstimateMinutes(Integer.toString(p.getTime()%60));
		pathForm.setHour(p.getDate());
		pathForm.setArrival_town_name(arrival);
		pathForm.setDepart_town_name(departure);
		pathForm.setDriver_name(driver);
		return pathForm;
	}
}
